package com.ruoyi.machine.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.machine.EmpMachineType;
import com.ruoyi.common.core.domain.machine.EmpDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.machine.mapper.EmpMachineTypeMapper;
import com.ruoyi.machine.mapper.EmpDictDataMapper;
import com.ruoyi.machine.service.IEmpMachineTypeService;
import com.ruoyi.system.service.ISysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 机台类型Service业务层处理
 *
 * @author 黄字添
 * @date 2024-01-09
 */
@Service
public class EmpMachineTypeServiceImpl implements IEmpMachineTypeService {
    private static final Logger log = LoggerFactory.getLogger(EmpMachineTypeServiceImpl.class);

    @Autowired
    private EmpMachineTypeMapper empMachineTypeMapper;

    @Autowired
    private EmpDictDataMapper empDictDataMapper;

    @Autowired
    private ISysRoleService roleService;
   
    @Autowired
    private Environment env;
    /**
     * 查询机台类型
     *
     * @param ID 机台类型主键
     * @return 机台类型
     */
    @Override
    public EmpMachineType selectEmpMachineTypeByID(Long ID) {
        return empMachineTypeMapper.selectEmpMachineTypeByID(ID);
    }

    /**
     * 查询机台类型列表
     *
     * @param empMachineType 机台类型
     * @return 机台类型
     */
    @Override
    @DataScope(deptAlias = "emt", userAlias = "emt")
    public List<EmpMachineType> selectEmpMachineTypeList(EmpMachineType empMachineType) {
//    	long uid = SecurityUtils.getLoginUser().getUserId();
//    	Long superRole = new Long(Integer.parseInt(env.getProperty("jurisdiction.super")));
//    	Long systemRole = new Long(Integer.parseInt(env.getProperty("jurisdiction.system")));
//    	Long agentRole = new Long(Integer.parseInt(env.getProperty("jurisdiction.agent")));
//    	Long storeRole = new Long(Integer.parseInt(env.getProperty("jurisdiction.store")));
//    	
//    	int type = 4;
//    	List<Long> list = roleService.selectRoleListByUserId(uid);
//    	for(int i = 0 ; i < list.size() ; i ++) {
//    		Long userRoleId = list.get(i);
//    		if(superRole.equals(userRoleId)) {
//    			type = 1;
//    			break;
//    		}else if (superRole.equals(agentRole) && type > 2) {
//    			type = 2;
//			}else if (superRole.equals(storeRole) && type > 3) {
//				type = 3;
//			}
//    	}

		return empMachineTypeMapper.selectEmpMachineTypeList(empMachineType);
    }

    /**
     * 获取机台类型字典
     */
    @Override
    public EmpDictData selectEmpMachineTypeDict() {
        return empDictDataMapper.selectMachineTypeDict();
    }

    /**
     * 新增机台类型
     *
     * @param empMachineType 机台类型
     * @return 结果
     */
    @Override
    public int insertEmpMachineType(EmpMachineType empMachineType) {
        return empMachineTypeMapper.insertEmpMachineType(empMachineType);
    }

    /**
     * 修改机台类型
     *
     * @param empMachineType 机台类型
     * @return 结果
     */
    @Override
    public int updateEmpMachineType(EmpMachineType empMachineType) {
        return empMachineTypeMapper.updateEmpMachineType(empMachineType);
    }

    /**
     * 批量删除机台类型
     *
     * @param IDs 需要删除的机台类型主键
     * @return 结果
     */
    @Override
    public int deleteEmpMachineTypeByIDs(Long[] IDs) {
        return empMachineTypeMapper.deleteEmpMachineTypeByIDs(IDs);
    }

    /**
     * 删除机台类型信息
     *
     * @param ID 机台类型主键
     * @return 结果
     */
    @Override
    public int deleteEmpMachineTypeByID(Long ID) {
        return empMachineTypeMapper.deleteEmpMachineTypeByID(ID);
    }

    @Override
    public EmpMachineType selectEmpMachineTypeByType(String empMachineType) {
        return empMachineTypeMapper.selectEmpMachineTypeByType(empMachineType);
    }

    @Override
    public List<EmpMachineType> selectEmpMachineTypeByIDs(Long[] typeIDs) {
        return empMachineTypeMapper.selectEmpMachineTypeByIDs(typeIDs);
    }

	@Override
	public List<EmpMachineType> selectEmpMachineTypeSortList(EmpMachineType empMachineType) {
		LoginUser user = SecurityUtils.getLoginUser();
		if(empMachineType.getDeptId() != null) {
        	empMachineType.setUserId(null);
        }else{
        	empMachineType.setUserId(user.getUserId());
        }
		
		List<EmpMachineType> machineTypeList = empMachineTypeMapper.getMachineType(empMachineType);
		
 		for(int i = 0 ; i < machineTypeList.size() ; i ++) {
 			EmpMachineType mt = machineTypeList.get(i);
 			if(mt.getSort() == null) {
 				empMachineTypeMapper.deleteByStoreId(mt.getSid());
 				for(EmpMachineType mct : machineTypeList) {
 					mct.setCreatedBy(user.getUsername());
 				}
 				
 				empMachineTypeMapper.insertIntoTypeSort(machineTypeList);
 				break;
 			}
		}
		
		return empMachineTypeMapper.getMachineType(empMachineType);
	}
}
